package org.grottarossa.tms.PSAPV.ui.home;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.ResourceBundle;

import org.grottarossa.template.data.pojo.BasePojo;
import org.grottarossa.template.data.pojo.query.Query;
import org.grottarossa.template.data.pojo.table.Record;
import org.grottarossa.template.drivers.DriverException;
import org.grottarossa.template.drivers.components.tokens.CaseManagementId;
import org.grottarossa.ui.DateHelper;
import org.grottarossa.ui.core.ApplicationSession;


/***
 * Gestisce lla logica della Home Page della console PSA
 * @author 
 *
 */
public class HomePagePresenter
{
	private final HomePage m_view;
	private final ApplicationSession m_appSession;
	private final ResourceBundle m_localizer;

	/*** Numero di elementi visitati per ogni query ***/
	private Hashtable<String, Integer> m_visitedItems;

	/*** Numero di elementi delle query ***/
	private Hashtable<String, Integer> m_totalItems;

	public HomePagePresenter(HomePage view, ApplicationSession appSession, ResourceBundle localizer)
	{
		this.m_view = view;
		this.m_appSession = appSession;
		this.m_localizer = localizer;
		m_view.setPresenter(this);
	}

	public void initialize() throws DriverException
	{
		m_visitedItems = new Hashtable<String, Integer>();
		m_totalItems = new Hashtable<String, Integer>();

		Hashtable<String, String> queries = m_appSession.getBuilder().getCaseManagement().
			enumQueriesScriptByCategory(m_localizer.getString("homequerycategory"),
					m_localizer.getString("homequeryapplication"));

		for(String query : queries.keySet())
		{
			Query result = m_appSession.getBuilder().getCaseManagement().
				executeQuery(queries.get(query), new HashMap<String, String>());

			int visitedRows = 0;

			for(Record r : result.getRecords())
			{
				Record record = m_appSession.getBuilder().getRecordManagement().
				getCaseObject((CaseManagementId)r.getCell(Query.CaseManagementIdFieldName).getValue());

				if(record.getCell(BasePojo.OBJECTREAD).getValue() != null)
					visitedRows++;
			}

			m_visitedItems.put(query, visitedRows);
			m_totalItems.put(query, result.getRecords().size());

			m_view.addQuery(result, query);

			refreshVisitedRowsCaptions();
		}
	}

	private void refreshVisitedRowsCaptions()
	{
		for(String query : m_visitedItems.keySet())
		{
			m_view.refreshVisitedRowsCaption(query, m_visitedItems.get(query), m_totalItems.get(query));
		}
	}

	public void onItemClicked(CaseManagementId caseId, String query)
	{
		try
		{
			Record record = m_appSession.getBuilder().getRecordManagement().getCaseObject(caseId);

			if(record.getCell(BasePojo.OBJECTREAD).getValue() == null)
				m_visitedItems.put(query, m_visitedItems.get(query) + 1);

			record.getCell(BasePojo.OBJECTREAD).setValue(DateHelper.DateToGregorianCalendar(new Date()));
			m_appSession.getBuilder().getRecordManagement().updateCaseObject(caseId, record);

			refreshVisitedRowsCaptions();
		}
		catch(Exception ex)
		{
			m_appSession.getNotifier().showError(ex);
		}
	}

}
